package com.benin.sms.extipetrpide

import android.annotation.SuppressLint
import androidx.core.text.isDigitsOnly
import com.benin.sms.valipetrpide.ValGlobalConstant
import java.io.File
import java.math.BigDecimal
import java.text.SimpleDateFormat
import java.util.*
import java.util.regex.Pattern

fun String.safeipetrpide(): String {
    if (this.isBlank()) {
        return ""
    }
    return this
}

fun String.safeIntipetrpide(): Int {
    if (this.isBlank()) {
        return 0
    }
    if (this.isDigitsOnly()) {
        return this.toInt()
    }
    return 0
}

fun String.prefixWithAddUnitipetrpide(): String {
    return ("+$this")
}

fun String.prefixWithMoneyUnitipetrpide(): String {
//    return (ValGlobalConstant.VAL_MONEY_FLAG + " " + this)
    return (this.toFinanceStringipetrpide() + " " + ValGlobalConstant.VAL_MONEY_FLAG)
}

fun String.suffixWithMoneyUnitipetrpide(): String {
    return (this.toFinanceStringipetrpide() + " " + ValGlobalConstant.VAL_MONEY_FLAG)
}

fun String.suffixWithDayUnitipetrpide(): String {
    return ("$this days")
}

fun String.toFinanceStringipetrpide(): String {
    if (this.isNotBlank()) {
        val resultDecimal: BigDecimal = BigDecimal(this).stripTrailingZeros()
        return resultDecimal.toPlainString()
    }
    return this
}

fun String.toInterestRateStringipetrpide(): String {
    if (this.isNotBlank()) {
        try {
            val valueDecimal: BigDecimal = BigDecimal(this).stripTrailingZeros()
            val edgeDecimal = BigDecimal(100)
            val resultDecimal: BigDecimal = valueDecimal.multiply(edgeDecimal)
            return ("${resultDecimal.stripTrailingZeros().toPlainString()}% per day")
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
    return this
}


fun String.toEncryptPhoneNumberipetrpide(): String {
    if (this.isNotBlank()) {
        val firstLine = if (this.length >= 3) this.substring(0, 3) else this
        val lastLine = if (this.length >= 6) this.substring(this.length - 3) else ""
        return "$firstLine **** $lastLine"
    }
    return this
}

fun String.filterForPhoneNumberipetrpide(): String {
    val regEx = "[^0-9]"
    val p = Pattern.compile(regEx)
    val m = p.matcher(this)
    return m.replaceAll("").trim()
}

fun String.isLinkipetrpide(): Boolean {
    return (this.safeipetrpide().startsWith("http"))
}

fun String.isEmailFormatipetrpide(): Boolean {
    if (this.safeipetrpide().isNotBlank()) {
        return this.contains("@")
    }
    return false
}

fun String.suffixPathipetrpide(): String {
    try {
        val file = File(this)
        return file.name
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return this
}

@SuppressLint("SimpleDateFormat")
fun String.dateForFormatString(targetFormat: String): Date? {
    try {
        val sDateFormat = SimpleDateFormat(targetFormat)
        val sdate = sDateFormat.parse(this)
        return sdate
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return null
}
